//
//  QZTimeConversion.m
//  QunZhu
//
//  Created by 超群 on 15/8/18.
//  Copyright (c) 2015年 qunzhu.Team. All rights reserved.
//

#import "QZTimeConversion.h"

@implementation QZTimeConversion


/**计算指定时间与当前的时间差*/

+ (NSString *)TimeString:(NSString*)timeStr {
    NSString *time = timeStr;
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yy-MM-dd HH:mm"];//HH:mm:ss
    NSDate *dt = [NSDate dateWithTimeIntervalSince1970:[time floatValue] / 1000];
   // NSString *dateString = [dateFormat stringFromDate:dt];
    
    NSTimeInterval  timeInterval = [dt timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    } else if((temp = timeInterval / 60) < 60){
        result = [NSString stringWithFormat:@"%ld分钟前", temp];
    } else if((temp = temp / 60) < 4) {
        result = [NSString stringWithFormat:@"%ld小时前", temp];
    } else {
        if((temp = temp / 24) < 30) {
            [dateFormat setDateFormat:@"MM-dd"];//HH:mm:ss
        } else if((temp = temp / 30) < 12) {
            [dateFormat setDateFormat:@"MM-dd"];//HH:mm:ss
        } else {
            temp = temp / 12;
            [dateFormat setDateFormat:@"yy-MM-dd"];//HH:mm:ss
        }
        result = [dateFormat stringFromDate:dt];
    }
    
    return result;
}
/**计算指定时间与当前的时间差*/
+ (NSString *)NumberString:(NSNumber*)numberStr {
    NSString *numberString = [[NSString alloc]init];
    int number = [numberStr intValue];
    if (number >= 1000 && number < 10000) {
        if (number%1000 == 0) {
            numberString = [NSString stringWithFormat:@"%dk",number/1000];
        } else if(number%100 == 0){
            numberString = [NSString stringWithFormat:@"%d.%dk",number/1000,(number%1000)/100];
        } else {
            numberString = [NSString stringWithFormat:@"%d.%dk",number/1000,(number%1000)/100+1];
        }
    } else if (number >= 10000) {
        if (number%10000 == 0) {
            numberString = [NSString stringWithFormat:@"%dm",number/10000];
        } else if(number%1000 == 0){
            numberString = [NSString stringWithFormat:@"%d.%dm",number/10000,(number%10000)/1000];
        } else {
            numberString = [NSString stringWithFormat:@"%d.%dm",number/10000,(number%10000)/1000+1];
        }
    } else {
        numberString = [NSString stringWithFormat:@"%d",number];
    }
    
    return numberString;
}

+ (NSString *)getTimeString:(NSString*)timeStr {
    NSString *time = timeStr;
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy.MM.dd"];//HH:mm:ss
    NSDate *dt = [NSDate dateWithTimeIntervalSince1970:[time floatValue] / 1000];
    NSString *dateString = [dateFormat stringFromDate:dt];
    return dateString;
}

+(NSString*)getRankNumber:(NSString*)rankNumberStr
{
    NSString* str;
    int a = [rankNumberStr intValue];
    switch (a) {
        case 1:{
            str = @"一级";
        }
            break;
        case 2:{
            str = @"二级";
        }
            break;
        case 3:{
            str = @"三级";
        }
            break;
            
        default:
            str = @"三级";
            break;
    }
    return str;
}

//时间戳转时间的方法:
+(NSString *)getDateFromString:(NSString *)timeString;
{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY.MM.dd"];//设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone* localzone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:localzone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timeString integerValue]];
    NSLog(@"confromTimesp  = %@",confromTimesp);
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    NSLog(@"confromTimespStr =  %@",confromTimespStr);
    return confromTimespStr;
}


//获得当前时间
+ (NSString *)nowTime
{
    NSDate* now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    comps = [calendar components:unitFlags fromDate:now];
    int hour = (int)[comps hour];
    int min = (int)[comps minute];
    int sec = (int)[comps second];
    int sum = (hour*3600+min*60+sec)*1000;
    NSString *time = [NSString stringWithFormat:@"%d",sum];
    return time;
}














@end
